/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import blueprint4j.utils.BindException;
import blueprint4j.utils.BindStringSelectInterface;
import blueprint4j.utils.Log;
import blueprint4j.utils.VectorString;
import blueprint4j.utils.VectorUniqueString;

public class DataPairFieldStringSelect extends DataPairFieldStringCombo implements ActionListener {
    
    private BindStringSelectInterface field;
    
    public DataPairFieldStringSelect(Binder binder,BindStringSelectInterface p_field,String [] display,String[] values,boolean editable)
    throws BindException {
        super(binder,p_field,display,values,editable);
        this.field = p_field;
        combo_field.addActionListener(this);
        try {
            refresh();
        } catch (BindException be) {
            Log.debug.out(be);
        }
    }
    
    protected String getSelectedItem() {
        if ("refresh...".equals(combo_field.getSelectedItem())) {
            return null;
        }
        return super.getSelectedItem();
    }
    
    public BindStringSelectInterface getField() {return field;}
    
    
    public synchronized void refresh() throws BindException {
        try {
            // SOME NEW FIELDS MIGHT HAVE BEEN ADDED TO COMBO FIELD
            VectorString combo_items = new VectorString();
            for (int i =0;i < combo_field.getItemCount();i++) {
                combo_items.add(combo_field.getItemAt(i).toString());
            }
            combo_field.removeAllItems();
            field.refresh();
            display = new VectorUniqueString(field.getDisplays());
            values = new VectorString(field.getOptions());
            display.add("refresh...");
            values.add("refresh...");
            for (int i =0;i < combo_items.size();i++) {
                if (!display.contains(combo_items.get(i))) {
                    display.add(combo_items.get(i));
                    values.add(combo_items.get(i));
                }
            }
            for (int i = 0;i < display.size();i++) {
                combo_field.addItem(display.get(i));
            }
            combo_field.setSelectedItem(null);
        } catch (Exception exception) {
            throw new BindException(exception);
        }
    }
    
    public void actionPerformed(ActionEvent event) {
        if ("refresh...".equals(combo_field.getSelectedItem())) {
            try {
                refresh();
            } catch (BindException be) {
                Log.debug.out(be);
            }
        }
    }
}

